package com.guaika.reggie.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guaika.reggie.common.ResultMessage;
import com.guaika.reggie.common.StringChars;
import com.guaika.reggie.dto.PermissionsDto;
import com.guaika.reggie.entity.Permissions;
import com.guaika.reggie.mapper.PermissionsMapper;
import com.guaika.reggie.service.PermissionsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author GuaiKa
 */
@Service
public class PermissionsServiceImpl extends ServiceImpl<PermissionsMapper,Permissions> implements PermissionsService {
    @Autowired
    private PermissionsMapper permissionsMapper;

    @Override
    public ResultMessage<String> savePermissions(PermissionsDto permissionsDto) {
        Long empId = permissionsDto.getEmpId();
        //清除之前的权限
        permissionsMapper.clearPermissions(empId);

        //保存修改的权限
        for (String permission : permissionsDto.getPermissions()) {
            String[] method = permission.split(" ");
            Permissions permissions = new Permissions();
            permissions.setEmployeeId(empId);
            permissions.setMethodType(method[0]);
            permissions.setUrl(method[1]);
            permissionsMapper.insert(permissions);
        }
        return ResultMessage.success(StringChars.UPDATE_SUCCESS.getStr());
    }

    @Override
    public ResultMessage<List<Permissions>> userPermissions(Long empId) {
        LambdaQueryWrapper<Permissions> wrapper = new LambdaQueryWrapper();
        wrapper.eq(Permissions::getEmployeeId,empId);
        return ResultMessage.success(permissionsMapper.selectList(wrapper));
    }
}
